﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using DevExpress.XtraEditors;


namespace PRE.Catalog
{
    public partial class FrmCapnhatphongban : PRE.Catalog.FrmBase
    {
        #region Variable
        DAL.EntitiesDAL.PhongbanDAL _dal = new DAL.EntitiesDAL.PhongbanDAL();
        BLL.EntitiesBLL.PhongbanBLL _bll = new BLL.EntitiesBLL.PhongbanBLL();
      
        DataTable _dtb = new DataTable(); 
        #endregion

        #region Inits
        public FrmCapnhatphongban()
        {
            InitializeComponent();
        }
        #endregion

        #region Form Load
        private void grcPhongban_Load(object sender, EventArgs e)
        {
            PerformRefresh();
        }
        #endregion

        #region Override
        protected override void PerformAdd()
        {
                    ReadOnlyControl(false);
                    ClearDataBindings();
                    ChangeStatus(false);

                    txt_Maphongban.Focus();
               
            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var id1 = grvPhongban.GetFocusedRowCellValue("MAPHONGBAN") + "";

            DialogResult result;
            var ok = _dal.Delete(id1);
            result = XtraMessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    XtraMessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            txt_Maphongban.Properties.ReadOnly = true;
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            ResetText();

            bto_Search.Visible = false;

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {

            var name_Tenphongban = Proper(txt_Tenphongban.Text);
            if (IsAdd)
            {
                var o = new DAL.Entities.Phongban()
                {
                    MAPHONGBAN = Convert.ToString(txt_Maphongban.Text),
                    TENPHONGBAN = name_Tenphongban

                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    // PerformAdd();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            if (IsEdit)
            {
                var id1 = grvPhongban.GetFocusedRowCellValue("MAPHONGBAN") + "";
                var o = new DAL.Entities.Phongban()
                {
                    MAPHONGBAN = Convert.ToString(txt_Maphongban.Text),
                    TENPHONGBAN = name_Tenphongban

                };

                var oki = _dal.Update(o);

                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txt_Maphongban.ResetText();
            txt_Tenphongban.ResetText();


            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txt_Maphongban.DataBindings.Clear();
            txt_Tenphongban.DataBindings.Clear();


            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {

            txt_Maphongban.DataBindings.Add("Text", _dtb, ".MAPHONGBAN");
            txt_Tenphongban.DataBindings.Add("Text", _dtb, ".TENPHONGBAN");


            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            txt_Maphongban.Properties.ReadOnly = isReadOnly;
            txt_Tenphongban.Properties.ReadOnly = isReadOnly;

            grcPhongban.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            ChangeStatus(false);
            ReadOnlyControl(false);
            //ResetText();
            bto_Search.Visible = true;

            grcPhongban.Enabled = true;
            txt_Maphongban.Focus();

            base.PerformSearch();
        }

        /// <summary>
        /// Execute Search data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bto_Search_Click(object sender, EventArgs e)
        {
            var name_Tenphongban = Proper(txt_Tenphongban.Text);

            var o = new DAL.Entities.Phongban()
            {
                MAPHONGBAN = Convert.ToString(txt_Maphongban.Text),
                TENPHONGBAN = name_Tenphongban

            };

            grcPhongban.DataSource = _dal.Search(o);
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }
    
        protected override void LoadData()
        {
            try
            {
                _dtb = _dal.Select();
              
                if (_dtb != null)
                {
                    grcPhongban.DataSource = _dtb;

                    AppGridView.SummaryField(grvPhongban, "MAPHONGBAN", "Tổng = {0:#,0} Phòng", DevExpress.Data.SummaryItemType.Count);

                    grvPhongban.OptionsView.ColumnAutoWidth = true;

                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("Lỗi : " + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.LoadData();
        }

        #endregion
 
    }
}
